package com.atguigu.day19.steam;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Stream;

/*
流式编程: 目的写出简洁高效的代码
  创建:
      集合
      数组
      Stream.of()有限
      Stream.generate()无限
  中间
  终结



 */
public class CreateStream {
    @Test
    public void test05(){
     //无限流
        Stream
         /*       .generate(new Supplier<Double>() {
            @Override
            public Double get() {
                return Math.random();
            }
        })*/
               // .generate(()->Math.random())
                .generate(Math::random)

                .forEach(System.out::println);

    }


    @Test
    public void test04(){
     //有限流
        Stream.of("张三","李四","王五","赵六","陈一").forEach(System.out::println);

    }


    @Test
    public void test03(){
         //数组
     int[]arr = {10,20,30,66,55,44,33};

        Arrays.stream(arr).forEach(System.out::println);
    }



    @Test
    public void test02(){
        //并行流 了解
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list, "张三", "李四", "王五", "赵六", "陈一");

        //list.parallelStream().forEach(System.out::println);
        list.parallelStream().forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s+","+Thread.currentThread().getName());
            }
        });
    }


    @Test
    public void test01() {
        //集合转为流
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list, "张三", "李四", "王五", "赵六", "陈一");

       // list.stream().forEach(System.out::println);

        list.stream().forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s+","+Thread.currentThread().getName());
            }
        });

    }

}
